﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Toenda.TFL.Base;
using Toenda.Rails.System;
using Toenda.Rails.DataContainer;

public partial class Controls_Admin_ctrl_deleteimage : System.Web.UI.UserControl {
	private AccountProvider ap = new AccountProvider();
	private StaticProvider sp = new StaticProvider();
	private ImageProvider ip = new ImageProvider();

	/// <summary>
	/// Page_Load
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e) {
		if(Session.Count > 0) {
			Account acc = ap.GetAccountBySession(Session["Zappr"].ToString());

			if(acc.Type == 2) {
				lblTitle.Text = GetLocalResourceObject("All").ToString();
				List<ImageItem> list = ip.GetImageList();

				gvJobs.DataSource = list;
				gvJobs.AllowPaging = true;
				gvJobs.PagerSettings.Mode = PagerButtons.NumericFirstLast;
				gvJobs.PagerSettings.PageButtonCount = 3;
				gvJobs.PageIndexChanging += new GridViewPageEventHandler(gvJobs_PageIndexChanging);
				gvJobs.PageSize = 25;
				gvJobs.DataBind();
			}
			else {
				Response.Redirect("~/login.aspx");
			}
		}
		else {
			Response.Redirect("~/login.aspx");
		}
	}

	/// <summary>
	/// gvJobs_PageIndexChanging
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void gvJobs_PageIndexChanging(object sender, GridViewPageEventArgs e) {
		gvJobs.PageIndex = e.NewPageIndex;
		gvJobs.DataBind();
	}

	/// <summary>
	/// DeleteImage
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void DeleteImage(object sender, CommandEventArgs e) {
		ImageItem item = ip.GetImageByUniqueID(e.CommandArgument.ToString());

		FileInfo info = new FileInfo(Server.MapPath("Images") + "\\" + item.ImageOriginal);
		info.Delete();

		info = new FileInfo(Server.MapPath("Images") + "\\" + item.Image1024);
		info.Delete();

		info = new FileInfo(Server.MapPath("Images") + "\\" + item.Image500);
		info.Delete();

		info = new FileInfo(Server.MapPath("Images") + "\\" + item.Image240);
		info.Delete();

		info = new FileInfo(Server.MapPath("Images") + "\\" + item.Image100);
		info.Delete();

		info = new FileInfo(Server.MapPath("Images") + "\\" + item.Image75x75);
		info.Delete();

		ip.DeleteImage(e.CommandArgument.ToString());
		
		Response.Redirect("~/default.aspx?id=_deleteimage");
	}
}
